<script setup>
import {ref,watch,onMounted} from 'vue'
import { queryPageApi,addEmpApi,queryEmpByIdApi,updateEmpApi,delEmpByIdApi} from '../../api/emp'
import {queryAllApi} from '../../api/dept'
import { ElMessage,ElMessageBox } from 'element-plus'


//获取token值
const token = ref('')

const getToken = () =>{
  const loginUser = JSON.parse(localStorage.getItem('loginUser'))
  token.value = loginUser.token
}



// 搜索栏表单对象
const searchEmp = ref({name:'',gender:'',date:[],begin:'',end:''})

// 元数据
const gender = ([{name:'男',value:'1'},{name:'女',value:'2'}])
const jobs = ([{name:'班主任',value:'1'},{name:'讲师',value:'2'},{name:'学工主管',value:'3'},{name:'教研主管',value:'4'},{name:'咨询师',value:'5'},{name:'其他',value:'6'}])

// 部门数据
const depts = ref([])

//钩子函数
onMounted(() => {
  search()  
  getToken()
})

//加载部门数据
onMounted(async() => {
  const result = await queryAllApi()
  if(result.code){
    depts.value = result.data
  }
})

// watch侦听函数
watch(()=>{return searchEmp.value.date},(newValue,oldValue)=>{
  if(newValue.length == 2){
    searchEmp.value.begin = newValue[0]
    searchEmp.value.end = newValue[1]
  }else{
    searchEmp.value.begin = ''
    searchEmp.value.end = ''
  }
})
// 搜索栏的查询按钮
const search = async() =>{
  const result = await queryPageApi(searchEmp.value.name,searchEmp.value.gender,searchEmp.value.begin,
                              searchEmp.value.end,currentPage.value,pageSize.value)
  empList.value = result.data.rows
  total.value = result.data.total
}
// 搜索栏的清空按钮
const clear = ()=>{
  searchEmp.value = {name:'',gender:'',date:[]}
  search()
}
// 表格数据
const empList = ref([])
// 分页条数据
const currentPage = ref(1) // 展示第几页
const pageSize = ref(10)  // 每页展示多少条数据
const background = ref(true) // 背景色
const total = ref(0) //总记录数
// 页面展示数发生变化时触发
const handleSizeChange = (val) => {
  search()
  console.log(`${val} 条数据`)
}
// 页码发生变化时触发
const handleCurrentChange = (val) => {
  search()
  console.log(`当前页码: ${val}`)
}


//新增员工
const addEmp = () => {
  dialogVisible.value = true
  dialogTitle.value = '新增员工'
  employee.value = ({
    username: '',
    name: '',
    gender: '',
    phone: '',
    job: '',
    salary: '',
    deptId: '',
    entryDate: '',
    image: '',
    exprList: []
})
  // 清除校验
   if(empFormRef.value){
    empFormRef.value.resetFields()
  }
}

//新增/修改表单
// 员工对象
const employee = ref({
  username: '',
  name: '',
  gender: '',
  phone: '',
  job: '',
  salary: '',
  deptId: '',
  entryDate: '',
  image: '',
  exprList: []
})

// 控制弹窗
const dialogVisible = ref(false)
const dialogTitle = ref('新增员工')

//文件上传
// 图片上传成功后触发
const handleAvatarSuccess = (response) => {  
  employee.value.image = response.data
}
// 文件上传之前触发
const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png') {
    ElMessage.error('只支持上传图片')
    return false
  } else if (rawFile.size / 1024 / 1024 > 10) {
    ElMessage.error('只能上传10M以内图片')
    return false
  }
  return true
}

// 添加工作经历
const addExpr = () =>{
  employee.value.exprList.push({company:'',job:'',begin:'',end:'',exprDate:[]})
}
// 删除工作经历
const delExpr = (index) =>{
  employee.value.exprList.splice(index,1)
}

// 侦听 exprList
watch(()=>{return employee.value.exprList},(newValue,oldValue)=>{
  if(employee.value.exprList.length > 0 && employee.value.exprList){
    employee.value.exprList.forEach((expr)=>{
      expr.begin = expr.exprDate[0]
      expr.end = expr.exprDate[1]
    })
  }  
},{deep: true})

const empFormRef = ref()

// 保存员工信息
const save = async () =>{
  empFormRef.value.validate(async (valid) =>{
    if(valid) {
      let result
      if(employee.value.id){
        // 修改操作
      result = await updateEmpApi(employee.value)
      }else{
        // 添加操作
      result = await addEmpApi(employee.value)
      }
      if(result.code){
        ElMessage.success('操作成功')  
        dialogVisible.value = false
        search()
      }else{
        ElMessage.error(result.msg)
      } 
    }else{
      ElMessage.error('表单校验不通过')
    }
  })
}
// 表单校验规则
// 验证规则
const rules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '用户名长度应在2到20个字符之间', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 2, max: 10, message: '姓名长度应在2到10个字符之间', trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1\d{10}$/g, message: '请输入有效的手机号', trigger: 'blur' }
  ]
});

// 编辑员工信息
const edit = async (id) => {
  const result = await queryEmpByIdApi(id)
  if(result.code){
    dialogTitle.value = '修改员工信息'
    dialogVisible.value = true
    employee.value = result.data
   // 转换gender和job为字符串
   if (employee.value.gender !== null && employee.value.gender !== undefined) {
     employee.value.gender = employee.value.gender.toString()
   }
   if (employee.value.job !== null && employee.value.job !== undefined) {
     employee.value.job = employee.value.job.toString()
   }
// 对工作经历进行处理
    let exprList = employee.value.exprList
    if(exprList.length > 0 && exprList){
      exprList.forEach(expr => {
        expr.exprDate = [expr.begin,expr.end]
      })
    }
  }
}

// 删除单条数据
const del = async (id) =>{
  ElMessageBox.confirm('您确定要删除信息吗？','提示',
    {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning'}
  ).then(async() => {    // '确定'之后的操作
    const result = await delEmpByIdApi(id)    
    if(result.code){
      ElMessage.success('删除成功')
      search()
    }else{
      ElMessage.error(result.msg)
    }
    }).catch(() => {  // '取消'之后的操作
      ElMessage.info('您已取消删除')
    })
}

// 批量选择数据
const selectionIds = ref([])
const handleSelectionChange = (selection) => {
  selectionIds.value = selection.map(item => item.id)
}
// 批量删除数据
//批量删除
const delIds = () => {
  ElMessageBox.confirm('您确定要删除信息吗？','提示',
    {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning'}
  ).then(async() => {    // '确定'之后的操作
    if(selectionIds.value && selectionIds.value.length > 0){
      const result = await delEmpByIdApi(selectionIds.value)    
          if(result.code){
            ElMessage.success('删除成功')
            search()
          }else{
            ElMessage.error(result.msg)
          }
    }else{
      ElMessage.info('您没有选取任何内容')
    }
    }).catch(() => {  // '取消'之后的操作
      ElMessage.info('您已取消删除')
    })
}
</script>

<template>
  <h1>员工管理</h1>
  <!-- 搜索栏 -->
   <div class="style">
  <el-form :inline="true" :model="searchEmp" class="demo-form-inline">

    <el-form-item label="姓名">
      <el-input v-model="searchEmp.name" placeholder="请输入员工姓名"  />
    </el-form-item>

    <el-form-item label="性别">
      <el-select
        v-model="searchEmp.gender"
        placeholder="请选择"
      >
        <el-option label="男" value="1" />
        <el-option label="女" value="2" />
      </el-select>
    </el-form-item>

    <el-form-item label="入职时间">
      <el-date-picker
        v-model="searchEmp.date"
        type="daterange"
        range-separator="到"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        value-format="YYYY-MM-DD"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="search">查询</el-button>
      <el-button type="info" @click="clear">清空</el-button>
    </el-form-item>
  </el-form>
</div>

    <!-- 功能按钮  -->
  <div class="style">
    <el-button type="primary" @click="addEmp">+ 新增员工</el-button>
    <el-button type="danger" @click="delIds">- 批量删除</el-button>
  </div>

  <!-- 表格 -->
  <div class="style">
    <el-table :data="empList" border style="width: 100%" @selection-change="handleSelectionChange">
    <el-table-column type="selection" prop="id" label="序号" width="50"  align="center"/>
    <el-table-column prop="name" label="姓名" width="120" align="center"/>
    <el-table-column prop="gender" label="性别" width="120" align="center">
      <template #default="scope">
      {{ scope.row.gender == 1 ?  '男':'女' }}
      </template>
    </el-table-column>
    <el-table-column prop="image" label="头像" width="120" align="center">
      <template #default="scope">
        <img :src="scope.row.image" height="40px"></img>
      </template>
    </el-table-column>
    <el-table-column prop="deptName" label="所属部门" width="120" align="center"/>
    <el-table-column prop="job" label="职位" width="120" align="center">
      <template #default="scope">
        <span v-if="scope.row.id == 1">班主任</span>
        <span v-else-if="scope.row.id == 2">讲师</span>
        <span v-else-if="scope.row.id == 3">学工主管</span>
        <span v-else-if="scope.row.id == 4">教研主管</span>
        <span v-else-if="scope.row.id == 5">咨询师</span>
        <span v-else>其他</span>
      </template>
    </el-table-column>
    <el-table-column prop="entryDate" label="入职日期" width="150" align="center"/>
    <el-table-column prop="updateTime" label="最后操作时间" width="200"align="center"/>
    <el-table-column prop="" label="操作" align="center">
      <template #default="scope">
        <el-button type="primary" @click="edit(scope.row.id)">编辑</el-button>
        <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  </div>
  
  <!-- 分页条 -->
  <div class="style">
    <el-pagination
      v-model:current-page="currentPage"  
      v-model:page-size="pageSize"
      :page-sizes="[5,10, 20, 50, 100]"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>

   <!-- 新增/修改员工的对话框 -->
  <el-dialog v-model="dialogVisible" :title="dialogTitle" >
      <el-form ref="empFormRef" :rules="rules"  :model="employee" label-width="80px">
        <!-- 基本信息 -->
        <!-- 第一行 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="employee.username" placeholder="请输入员工用户名，2-20个字"></el-input>
            </el-form-item>
          </el-col>
          
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="employee.name" placeholder="请输入员工姓名，2-10个字"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第二行 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="employee.gender" placeholder="请选择性别" style="width: 100%;">
                <el-option v-for=" g in gender" :key="g.value" :label="g.name" :value="g.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="employee.phone" placeholder="请输入员工手机号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第三行 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="职位">
              <el-select v-model="employee.job" placeholder="请选择职位" style="width: 100%;">
                <el-option v-for="j in jobs" :key="j.value" :label="j.name" :value="j.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="薪资">
              <el-input v-model="employee.salary" placeholder="请输入员工薪资"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第四行 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所属部门">
              <el-select v-model="employee.deptId" placeholder="请选择部门" style="width: 100%;">
                <el-option v-for="d in depts" :key="d.id" :label="d.name" :value="d.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入职日期">
              <el-date-picker v-model="employee.entryDate" type="date" style="width: 100%;" placeholder="选择日期" format="YYYY-MM-DD" value-format="YYYY-MM-DD"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第五行 -->
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="头像">
              <el-upload
                class="avatar-uploader"
                action="/api/upload"
                :headers="{'token':token}"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                >
                <img v-if="employee.image" :src="employee.image" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 工作经历 -->
        <!-- 第六行 -->
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="工作经历">
              <el-button type="success" size="small" @click="addExpr">+ 添加工作经历</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        
        <!-- 第七行 ...  工作经历 -->
        <el-row :gutter="3" v-for="(expr, index) in employee.exprList">
          <el-col :span="10">
            <el-form-item size="small" label="时间"  label-width="80px">
              <el-date-picker v-model="expr.exprDate" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" format="YYYY-MM-DD" value-format="YYYY-MM-DD" ></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item size="small" label="公司" label-width="60px">
              <el-input v-model="expr.company" placeholder="请输入公司名称"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item size="small" label="职位" label-width="60px">
              <el-input v-model="expr.job" placeholder="请输入职位"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="2">
            <el-form-item size="small" label-width="0px">
              <el-button type="danger" @click="delExpr(index)">- 删除</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      
        <!-- 底部按钮 -->
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="save">保存</el-button>
          </span>
        </template>

    </el-dialog>


</template>

<style scoped>
.style{
  margin: 15px;
}

avatar {
  height: 40px;
}
.avatar-uploader .avatar {
  width: 78px;
  height: 78px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 78px;
  height: 78px;
  text-align: center;
  /* 添加灰色的虚线边框 */
  border: 1px dashed var(--el-border-color);
}
</style>